package controllDAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import controllBen.Eventos;
import controllBen.InterfaceCRUD;

public class DaoEventos extends DaoConnection implements InterfaceCRUD<Eventos> {

	public DaoEventos() throws ClassNotFoundException {
		super();
	}

	Connection conn = null;
	PreparedStatement ps = null;
	String sql = null;
	ResultSet rs = null;

	@Override
	public void insert(Eventos objeto) {
		sql = "INSERT INTO eventos(id, titulo, descricao) VALUES (NEXTVAL('seq_eventos'), ?, ?)";
		try {
			conn = getConnection();
			ps = conn.prepareStatement(sql);

			ps.setString(1, objeto.getTitulo());
			ps.setString(2, objeto.getDescricao());

			ps.execute();

			ps.close();
			conn.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void update(Eventos objeto) {
		sql = "UPDATE eventos set  titulo= ?, descricao = ? WHERE id = ?";
		try {
			conn = getConnection();
			ps = conn.prepareStatement(sql);

			ps.setString(1, objeto.getTitulo());
			ps.setString(2, objeto.getDescricao());

			ps.setInt(3, objeto.getId());
			ps.execute();

			ps.close();
			conn.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void delete(int id) {
		sql = "DELETE from eventos WHERE id = ?";

		try {
			conn = getConnection();
			ps = conn.prepareStatement(sql);

			ps.setInt(1, id);	
			
			ps.execute();
			
			ps.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Eventos getId(int id) {
		sql = "select id, titulo, descricao from eventos where id = ?";
		Eventos evento = new Eventos();
		try {
			conn = getConnection();
			ps = conn.prepareStatement(sql);

			ps.setInt(1, id);

			rs = ps.executeQuery();
			if (rs.next()) {
				evento.setId(rs.getInt("id"));
				evento.setTitulo(rs.getString("titulo"));
				evento.setDescricao(rs.getString("descricao"));
			}
			
			ps.close();
			conn.close();
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return evento;
	}

	@Override
	public ArrayList<Eventos> listaTodos() {
		sql = "select id, titulo, descricao from eventos";
		ArrayList<Eventos> lista = new ArrayList<>();
		Eventos eventos;
		try {
			conn = getConnection();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()){
				eventos = new Eventos();
				eventos.setId(rs.getInt("id"));
				eventos.setTitulo(rs.getString("titulo"));
				eventos.setDescricao(rs.getString("descricao"));
				lista.add(eventos);
			}
			
			conn.close();
			ps.close();
			rs.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return lista;
	}

	@Override
	public ArrayList<Eventos> getData() {
		// TODO Auto-generated method stub
		return null;
	}

}
